/* 
 * File:   UsersControl.cpp
 * Author: Neo Siong Wei
 * 
 * Created on February 7, 2014, 6:08 PM
 */

#include "UsersControl.h"
#include "MailControl.h"

UsersControl::UsersControl() {

    load_UsersVector();

}

UsersControl::~UsersControl() {

    write_UsersVector();
}

void UsersControl::load_UsersVector() {

    fstream readFile;

    bool tempFlag;
    string tempRole, tempUsername, tempPassword;

    readFile.open("UsersData.txt", ios::in);

    while (readFile >> tempRole >> tempUsername >> tempPassword >> tempFlag) {


        UsersData temp(decrypt(tempRole), decrypt(tempUsername), tempPassword, tempFlag);

        users_Vector.push_back(temp);

    }

    readFile.close();

}

void UsersControl::write_UsersVector() {

    fstream outfile;

    outfile.open("UsersData.txt", ios::out);

    for (unsigned int i = 0; i < users_Vector.size(); i++) {
        outfile << encrypt(users_Vector[i].getID())
                << "\t"
                << encrypt(users_Vector[i].getUsername())
                << "\t"
                << users_Vector[i].getPassword()
                << "\t"
                << users_Vector[i].getStatus()
                << "\r\n";
    }

    outfile.close();

}

void UsersControl::view_Users(char role) {

    switch (role) {
        case 'G':
            cout << "\t| Viewing all Holiday Managers, Booking Staffs Finance Officers |" << endl;
            cout << "No.\tIdentity\tUsername" << endl;

            for (unsigned int i = 0; i < users_Vector.size(); i++) {

                if (users_Vector[i].getRole() == 'B' || users_Vector[i].getRole() == 'H' || users_Vector[i].getRole() == 'F') {

                    cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                }
            }

            cout << endl;
            break;
        case 'H':

            cout << "\t| Viewing all Clients |" << endl;
            cout << "No.\tIdentity\tUsername" << endl;
            for (unsigned int i = 0; i < users_Vector.size(); i++) {

                if (users_Vector[i].getRole() == 'C')
                    cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
            }

            cout << endl;
            break;
        case 'B':

            cout << "\t| Viewing all Clients |" << endl;
            cout << "No.\tIdentity\tUsername" << endl;
            for (unsigned int i = 0; i < users_Vector.size(); i++) {

                if (users_Vector[i].getRole() == 'C')
                    cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
            }

            cout << endl;

            break;
        default: cout << "You do not have such privilege!" << endl;
            break;
    }

}

void UsersControl::create_Users(char role) {

    string ID, username, password;
    int yourOption;

    switch (role) {
        case 'G':
        {

            cout << "1)\tCreate a Holiday Manager." << endl;
            cout << "2)\tCreate a Booking Staff." << endl;
            cout << "3)\tCreate a Finance Officer." << endl;
            cout << "9)\tBack to Menu." << endl;

            cout << "Please enter your choice: ";
            cin >> yourOption;

            switch (yourOption) {
                case 1:
                {
                    bool duplicated;

                    do {
                        duplicated = false;
                        cout << "Enter Holiday Manager's Username: ";
                        cin >> username;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getUsername() == username) {
                                duplicated = true;
                                cout << "Username existed!" << endl;
                            }

                        }
                    } while (duplicated);

                    cout << "Enter Holiday Manager's Password: ";
                    cin >> password;

                    string encryptedPassword = encrypt(password);

                    string ID;

                    ID = users_Vector[users_Vector.size() - 1].getID();

                    char *p = &ID[0];
                    *p = 'H';

                    char *q4 = &ID[4];
                    char *q3 = &ID[3];
                    char *q2 = &ID[2];
                    char *q1 = &ID[1];
                    *q4 += 1;

                    if (*q4 == ':') {
                        *q3 += 1;
                        *q4 = '0';

                        if (*q3 == ':') {
                            *q2 += 1;
                            *q3 = '0';
                        }
                        if (*q2 == ':') {
                            *q1 += 1;
                            *q2 = '0';
                        }
                    }



                    cout << "ID = " << ID << endl;


                    cout << "Holiday Manager " << username << " was created." << endl;
                    cout << "ID is " << ID << endl;

                    UsersData temp(ID, username, encryptedPassword, false);

                    users_Vector.push_back(temp);

                }
                    break;
                case 2:
                {
                    bool duplicated;

                    do {
                        duplicated = false;
                        cout << "Enter Booking Staff's Username: ";
                        cin >> username;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getUsername() == username) {
                                duplicated = true;
                                cout << "Username existed!" << endl;
                            }

                        }
                    } while (duplicated);

                    cout << "Enter Booking Staff's Password: ";
                    cin >> password;

                    string encryptedPassword = encrypt(password);

                    string ID;

                    ID = users_Vector[users_Vector.size() - 1].getID();

                    char *p = &ID[0];
                    *p = 'B';

                    char *q4 = &ID[4];
                    char *q3 = &ID[3];
                    char *q2 = &ID[2];
                    char *q1 = &ID[1];
                    *q4 += 1;

                    if (*q4 == ':') {
                        *q3 += 1;
                        *q4 = '0';

                        if (*q3 == ':') {
                            *q2 += 1;
                            *q3 = '0';
                        }
                        if (*q2 == ':') {
                            *q1 += 1;
                            *q2 = '0';
                        }
                    }

                    cout << "Booking Staff " << username << " was created." << endl;
                    cout << "ID is " << ID << endl;

                    UsersData temp(ID, username, encryptedPassword, false);

                    users_Vector.push_back(temp);

                }
                    break;
                case 3:
                {
                    bool duplicated;

                    do {
                        duplicated = false;
                        cout << "Enter Finance Officer's Username: ";
                        cin >> username;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getUsername() == username) {
                                duplicated = true;
                                cout << "Username existed!" << endl;
                            }

                        }
                    } while (duplicated);

                    cout << "Enter Finance Officer's Password: ";
                    cin >> password;

                    string encryptedPassword = encrypt(password);

                    string ID;

                    ID = users_Vector[users_Vector.size() - 1].getID();

                    char *p = &ID[0];
                    *p = 'F';

                    char *q4 = &ID[4];
                    char *q3 = &ID[3];
                    char *q2 = &ID[2];
                    char *q1 = &ID[1];
                    *q4 += 1;

                    if (*q4 == ':') {
                        *q3 += 1;
                        *q4 = '0';

                        if (*q3 == ':') {
                            *q2 += 1;
                            *q3 = '0';
                        }
                        if (*q2 == ':') {
                            *q1 += 1;
                            *q2 = '0';
                        }
                    }

                    cout << "Finance Officer " << username << " was created." << endl;
                    cout << "ID is " << ID << endl;

                    UsersData temp(ID, username, encryptedPassword, false);

                    users_Vector.push_back(temp);

                }
                    break;
                case 9: cout << "Back to Main Menu." << endl;
                    break;
                default: cout << "Invalid Option! Back to Main Menu." << endl;
                    break;
            }
            break;
        }
        case 'B':
            cout << "1)\tCreate a Client." << endl;
            cout << "9)\tBack to Menu." << endl;

            cout << "Please enter your choice: ";
            cin >> yourOption;


            switch (yourOption) {
                case 1:
                {

                    bool duplicated;

                    do {
                        duplicated = false;
                        cout << "Enter Client's Username: ";
                        cin >> username;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getUsername() == username) {
                                duplicated = true;
                                cout << "Username existed!" << endl;
                            }

                        }
                    } while (duplicated);

                    cout << "Enter Client's Password: ";
                    cin >> password;

                    string encryptedPassword = encrypt(password);

                    string ID;

                    ID = users_Vector[users_Vector.size() - 1].getID();

                    char *p = &ID[0];
                    *p = 'C';

                    char *q4 = &ID[4];
                    char *q3 = &ID[3];
                    char *q2 = &ID[2];
                    char *q1 = &ID[1];
                    *q4 += 1;

                    if (*q4 == ':') {
                        *q3 += 1;
                        *q4 = '0';

                        if (*q3 == ':') {
                            *q2 += 1;
                            *q3 = '0';
                        }
                        if (*q2 == ':') {
                            *q1 += 1;
                            *q2 = '0';
                        }
                    }

                    cout << "Client " << username << " was created." << endl;
                    cout << "ID is " << ID << endl;

                    UsersData temp(ID, username, encryptedPassword, false);

                    users_Vector.push_back(temp);

                    //createmailbox
                    mailController m;
                    m.createMailbox(ID);

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {
                        cout << users_Vector[i].getID() << endl;
                    }
                }
            }
            break;
        case 9: cout << "Back to Main Menu." << endl;
            break;
        default: cout << "You do not have such privilege!" << endl;
            break;
    }

}

void UsersControl::edit_Users(char role) {

    int yourOption;
    string editOption;

    switch (role) {
        case 'G':
        {

            cout << "1)\tEdit a Holiday Manager's username." << endl;
            cout << "2)\tEdit a Booking Staff's username." << endl;
            cout << "3)\tEdit a Finance Officer's username." << endl;
            cout << "9)\tBack to Menu." << endl;

            cout << "Please enter your choice: ";
            cin >> yourOption;

            switch (yourOption) {

                case 1:
                {
                    bool edited = false;
                    cout << "\t| Viewing all Holiday Managers |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'H') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {

                        cout << endl;

                        cout << "Edit which Holiday Manager's username?" << endl;
                        cout << "Enter username: ";
                        cin >> editOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'H' && editOption == users_Vector[i].getUsername()) {
                                edited = true;
                            }
                        }
                        if (!edited) {
                            cout << endl << "No such username!" << endl;
                        } else {
                            string username;
                            bool duplicated = false;

                            cout << "User " << editOption << " found. Enter new username: ";

                            do {

                                if (duplicated) {
                                    cout << "Username Existed! Please try another username: ";
                                }

                                cin >> username;

                                for (unsigned int i = 0; i < users_Vector.size(); i++) {

                                    if (users_Vector[i].getUsername() == username) {
                                        duplicated = true;
                                    }
                                }
                            } while (duplicated);

                            for (unsigned int i = 0; i < users_Vector.size(); i++) {
                                if (editOption == users_Vector[i].getUsername()) {
                                    users_Vector[i].setUsername(username);
                                    cout << "User " << editOption << " successfully changed to " << username << "." << endl;
                                }
                            }
                        }

                    } while (!edited);
                }
                    break;

                case 2:
                {
                    bool edited = false;
                    cout << "\t| Viewing all Booking Staffs |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'B') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {

                        cout << endl;

                        cout << "Edit which Booking Staff's username?" << endl;
                        cout << "Enter username: ";
                        cin >> editOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'B' && editOption == users_Vector[i].getUsername()) {
                                edited = true;
                            }
                        }
                        if (!edited) {
                            cout << endl << "No such username!" << endl;
                        } else {
                            string username;
                            bool duplicated = false;

                            cout << "User " << editOption << " found. Enter new username: ";

                            do {

                                if (duplicated) {
                                    cout << "Username Existed! Please try another username: ";
                                }

                                cin >> username;

                                for (unsigned int i = 0; i < users_Vector.size(); i++) {

                                    if (users_Vector[i].getUsername() == username) {
                                        duplicated = true;
                                    }
                                }
                            } while (duplicated);

                            for (unsigned int i = 0; i < users_Vector.size(); i++) {
                                if (editOption == users_Vector[i].getUsername()) {
                                    users_Vector[i].setUsername(username);
                                    cout << "User " << editOption << " successfully changed to " << username << "." << endl;
                                }
                            }
                        }

                    } while (!edited);
                }
                    break;

                case 3:
                {
                    bool edited = false;
                    cout << "\t| Viewing all Finance Officers |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'F') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {

                        cout << endl;

                        cout << "Edit which Finance Officers's username?" << endl;
                        cout << "Enter username: ";
                        cin >> editOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'F' && editOption == users_Vector[i].getUsername()) {
                                edited = true;
                            }
                        }
                        if (!edited) {
                            cout << endl << "No such username!" << endl;
                        } else {
                            string username;
                            bool duplicated = false;

                            cout << "User " << editOption << " found. Enter new username: ";

                            do {

                                if (duplicated) {
                                    cout << "Username Existed! Please try another username: ";
                                }

                                cin >> username;

                                for (unsigned int i = 0; i < users_Vector.size(); i++) {

                                    if (users_Vector[i].getUsername() == username) {
                                        duplicated = true;
                                    }
                                }
                            } while (duplicated);

                            for (unsigned int i = 0; i < users_Vector.size(); i++) {
                                if (editOption == users_Vector[i].getUsername()) {
                                    users_Vector[i].setUsername(username);
                                    cout << "User " << editOption << " successfully changed to " << username << "." << endl;
                                }
                            }
                        }

                    } while (!edited);
                }
                    break;
                case 9: cout << "Back to Main Menu." << endl;
                    break;
                default: cout << "Invalid Option! Back to Main Menu." << endl;
                    break;
            }
            break;
        }
        case 'B':
        {
            cout << "1)\tEdit a Client's username." << endl;
            cout << "9)\tBack to Menu." << endl;

            cout << "Please enter your choice: ";
            cin >> yourOption;

            switch (yourOption) {

                case 1:
                {
                    bool edited = false;
                    cout << "\t| Viewing all Clients |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'C') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {

                        cout << endl;

                        cout << "Edit which Client's username?" << endl;
                        cout << "Enter username: ";
                        cin >> editOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'C' && editOption == users_Vector[i].getUsername()) {
                                edited = true;
                            }
                        }
                        if (!edited) {
                            cout << endl << "No such username!" << endl;
                        } else {
                            string username;
                            bool duplicated = false;

                            cout << "User " << editOption << " found. Enter new username: ";

                            do {

                                if (duplicated) {
                                    cout << "Username Existed! Please try another username: ";
                                }

                                cin >> username;

                                for (unsigned int i = 0; i < users_Vector.size(); i++) {

                                    if (users_Vector[i].getUsername() == username) {
                                        duplicated = true;
                                    }
                                }
                            } while (duplicated);

                            for (unsigned int i = 0; i < users_Vector.size(); i++) {
                                if (editOption == users_Vector[i].getUsername()) {
                                    users_Vector[i].setUsername(username);
                                    cout << "User " << editOption << " successfully changed to " << username << "." << endl;
                                }
                            }
                        }

                    } while (!edited);
                }
                    break;

                case 9: cout << "Back to Main Menu." << endl;
                    break;
                default: cout << "Invalid Option! Back to Main Menu." << endl;
                    break;
            }

        }
            break;
        default: cout << "You do not have such privilege!" << endl;
            break;
    }
}

void UsersControl::delete_Users(char role) {

    int yourOption;
    string deleteOption;

    switch (role) {
        case 'G':
        {

            cout << "1)\tDelete a Holiday Manager." << endl;
            cout << "2)\tDelete a Booking Staff." << endl;
            cout << "3)\tDelete a Finance Officer." << endl;
            cout << "9)\tBack to Menu." << endl;

            cout << "Please enter your choice: ";
            cin >> yourOption;

            switch (yourOption) {

                case 1:
                {
                    bool deleted = false;
                    cout << "\t| Viewing all Holiday Managers |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'H') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {

                        cout << endl;

                        cout << "Delete which Holiday Manager?" << endl;
                        cout << "Enter username: ";
                        cin >> deleteOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'H' && deleteOption == users_Vector[i].getUsername()) {
                                cout << "User " << deleteOption << " successfully deleted!" << endl;
                                users_Vector.erase(users_Vector.begin() + i);
                                deleted = true;
                            }
                        }
                        if (!deleted) {
                            cout << endl << "No such username!" << endl;
                        }

                    } while (!deleted);
                }
                    break;
                case 2:
                {
                    bool deleted = false;
                    cout << "\t| Viewing all Booking Staffs |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'B') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {
                        cout << endl;

                        cout << "Delete which Booking Staff?" << endl;
                        cout << "Enter username: ";
                        cin >> deleteOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'B' && deleteOption == users_Vector[i].getUsername()) {
                                cout << "User " << deleteOption << " successfully deleted!" << endl;
                                users_Vector.erase(users_Vector.begin() + i);
                                deleted = true;

                            }
                        }
                        if (!deleted) {
                            cout << endl << "No such username!" << endl;
                        }

                    } while (!deleted);

                }
                    break;

                case 3:
                {

                    bool deleted = false;
                    cout << "\t| Viewing all Finance Officer |" << endl;
                    cout << "No.\tIdentity\tUsername" << endl;

                    for (unsigned int i = 0; i < users_Vector.size(); i++) {

                        if (users_Vector[i].getRole() == 'F') {

                            cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                        }
                    }
                    do {
                        cout << endl;

                        cout << "Delete which Finance Officer?" << endl;
                        cout << "Enter username: ";
                        cin >> deleteOption;

                        for (unsigned int i = 0; i < users_Vector.size(); i++) {

                            if (users_Vector[i].getRole() == 'F' && deleteOption == users_Vector[i].getUsername()) {
                                cout << "User " << deleteOption << " successfully deleted!" << endl;
                                users_Vector.erase(users_Vector.begin() + i);
                                deleted = true;

                            }
                        }
                        if (!deleted) {
                            cout << endl << "No such username!" << endl;
                        }

                    } while (!deleted);

                }
                    break;
                case 9: cout << "Back to Main Menu." << endl;
                    break;
                default: cout << "Invalid Option! Back to Main Menu." << endl;
                    break;
            }
        }
            break;
        case 'H':
        {

            bool deleted = false;
            cout << "\t| Viewing all Clients |" << endl;
            cout << "No.\tIdentity\tUsername" << endl;

            for (unsigned int i = 0; i < users_Vector.size(); i++) {

                if (users_Vector[i].getRole() == 'C') {

                    cout << i + 1 << ")\t" << users_Vector[i].getID() << "\t\t" << users_Vector[i].getUsername() << endl;
                }
            }
            do {
                cout << endl;

                cout << "Delete which Client?" << endl;
                cout << "Enter username: ";
                cin >> deleteOption;

                for (unsigned int i = 0; i < users_Vector.size(); i++) {

                    if (users_Vector[i].getRole() == 'C' && deleteOption == users_Vector[i].getUsername()) {
                        //delete mailbox
                        mailController m;
                        m.deleteMailbox(users_Vector[i].getID());

                        cout << "User " << deleteOption << " successfully deleted!" << endl;
                        users_Vector.erase(users_Vector.begin() + i);
                        deleted = true;
                    }
                }
                if (!deleted) {
                    cout << endl << "No such username!" << endl;
                }

            } while (!deleted);

        }

            cout << endl;
            break;
        default: cout << "You do not have such privilege!" << endl;
            break;
    }

}

string UsersControl::login() {

    string username;
    string password;
    int noOfAttempt = 1;

    /*fstream afile;
    string wcmsg;
    afile.open("loginWelcome.txt", ios::in);
    while (!afile.eof())
    {
        getline(afile, wcmsg);
        cout << wcmsg << endl;
    }
     */
    cout << endl;
    cout << "| Login System |" << endl;
    cout << "Enter username: ";
    cin >> username;
    cout << "Enter Password: ";
    cin >> password;

    string encryptedPassword = encrypt(password);

    for (int i = 0; i < users_Vector.size(); i++) {

        if (username == users_Vector[i].getUsername()) { // Username Matched

            if (users_Vector[i].getStatus() == 1) {
                cout << "Account Locked! Please contact the Administrator." << endl;
                return "Locked";
            }

            while (encryptedPassword != users_Vector[i].getPassword() && users_Vector[i].getStatus() == false) { // Password DOES NOT Matched

                if (noOfAttempt >= 3) {
                    cout << "User " << username << " was locked! Please contact the Administrator." << endl;
                    users_Vector[i].setStatusLocked();
                    return "Locked";
                }

                cout << "Wrong password! Please enter again. (Attempt Number " << noOfAttempt << ")" << endl;
                noOfAttempt++;
                cout << "Enter Password: ";
                cin >> password;
                encryptedPassword = encrypt(password);


            }

            return users_Vector[i].getID();

        }

    }
    return "DoNotExist";
}

string UsersControl::encrypt(string pw) {
    char *ptr = &pw[0];

    while (*ptr != '\0') {
        *ptr += 3;
        ptr++;
    }

    return pw;
}

string UsersControl::decrypt(string pw) {
    char *ptr = &pw[0];

    while (*ptr != '\0') {
        *ptr -= 3;
        ptr++;
    }

    return pw;
}